package com.webserver.controller;

import com.webserver.core.ClientHandler;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;

/**
 * MVC模型
 * M:model
 * V:view
 * C:controller
 * 处理与用户相关的操作
 */
public class UserController {

    private static File USER_DIR = new File("./users");

    private static File staticDir;
    static {
        if (!USER_DIR.exists()){
            USER_DIR.mkdirs();
        }

        try {
            staticDir = new File(
                    ClientHandler.class.getClassLoader().getResource(
                            "./static"
                    ).toURI()
            );
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    /**
     * 处理用户注册
     * @param request
     * @param response
     */
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理用户注册...");

        //1获取用户表单提供上来的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);

        if (username==null||password==null||nickname==null||ageStr==null||
        !ageStr.matches("[0-9]+")){
            File file = new File(staticDir,"/myweb/reg_fail.html");
            response.setContentFile(file);
            return;
        }

        //2将用户信息以一个User实例形式表示，并序列化到文件里
        int age = Integer.parseInt(ageStr);
        User user = new User(username,password,nickname,age);
        //将改用户信息以户名.obg的形式保存到user目录里
        File userFile = new File(USER_DIR,username+".obj");
        if (userFile.exists()){
            File file = new File(staticDir,"/myweb/have_user.html");
            response.setContentFile(file);
            return;
        }
        try(
                FileOutputStream fos = new FileOutputStream(userFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            oos.writeObject(user);
            //注册成功了
            File file = new File(staticDir,"/myweb/reg_success.html");
            response.setContentFile(file);
        } catch (IOException e){

        }

        System.out.println("处理用户注册完毕");
    }

    public void login(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始登录。。");
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        if (username==null||password==null) {
            File file = new File(staticDir,"/myweb/login_info_error.html");
            response.setContentFile(file);
            return;
        }

        File userFile = new File(USER_DIR,username+".obj");
        if (userFile.exists()){
            try(
                    FileInputStream fis = new FileInputStream(userFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ){
                User user =  (User) ois.readObject();
                if (user.getPassword().equals(password)){
                    File file = new File(staticDir,"/myweb/login_success.html");
                    response.setContentFile(file);
                    return;
                }
            }catch (IOException | ClassNotFoundException e){

            }
        }
        File file = new File(staticDir,"/myweb/login_fail.html");
        response.setContentFile(file);
        System.out.println("登陆成功");
    }

}
